home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CreatingGames / Utilities / C / Mesa / samples / point.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-12  |  4.7 KB  |  223 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include "gltk.h"
  29.  
  30.  
  31. #define CI_RED TK_RED
  32. #define CI_ANTI_ALIAS_GREEN 16
  33. #define CI_ANTI_ALIAS_YELLOW 32
  34. #define CI_ANTI_ALIAS_RED 48
  35.  
  36.  
  37. GLenum rgb, doubleBuffer, directRender, windType;
  38. GLint windW, windH;
  39.  
  40. GLenum mode;
  41. GLint size;
  42. float point[3] = {
  43.     1.0, 1.0, 0.0
  44. };
  45.  
  46.  
  47. static void Init(void)
  48. {
  49.     GLint i;
  50.  
  51.     glClearColor(0.0, 0.0, 0.0, 0.0);
  52.  
  53.     glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
  54.  
  55.     if (!rgb) {
  56.     for (i = 0; i < 16; i++) {
  57.         tkSetOneColor(i+CI_ANTI_ALIAS_RED, i/15.0, 0.0, 0.0);
  58.         tkSetOneColor(i+CI_ANTI_ALIAS_YELLOW, i/15.0, i/15.0, 0.0);
  59.         tkSetOneColor(i+CI_ANTI_ALIAS_GREEN, 0.0, i/15.0, 0.0);
  60.     }
  61.     }
  62.  
  63.     mode = GL_FALSE;
  64.     size = 1;
  65. }
  66.  
  67. static void Reshape(int width, int height)
  68. {
  69.  
  70.     windW = (GLint)width;
  71.     windH = (GLint)height;
  72.  
  73.     glViewport(0, 0, width, height);
  74.  
  75.     glMatrixMode(GL_PROJECTION);
  76.     glLoadIdentity();
  77.     gluOrtho2D(-windW/2, windW/2, -windH/2, windH/2);
  78.     glMatrixMode(GL_MODELVIEW);
  79. }
  80.  
  81. static GLenum Key(int key, GLenum mask)
  82. {
  83.  
  84.     switch (key) {
  85.       case TK_ESCAPE:
  86.     tkQuit();
  87.       case TK_1:
  88.     mode = !mode;
  89.     break;
  90.       case TK_W:
  91.     size++;
  92.     break;
  93.       case TK_w:
  94.     size--;
  95.     if (size < 1) {
  96.         size = 1;
  97.     }
  98.     break;
  99.       case TK_LEFT:
  100.     point[0] -= 0.25;
  101.     break;
  102.       case TK_RIGHT:
  103.     point[0] += 0.25;
  104.     break;
  105.       case TK_UP:
  106.     point[1] += 0.25;
  107.     break;
  108.       case TK_DOWN:
  109.     point[1] -= 0.25;
  110.     break;
  111.       default:
  112.     return GL_FALSE;
  113.     }
  114.     return GL_TRUE;
  115. }
  116.  
  117. static void Draw(void)
  118. {
  119.  
  120.     glClear(GL_COLOR_BUFFER_BIT);
  121.  
  122.     TK_SETCOLOR(windType, TK_YELLOW);
  123.     glBegin(GL_LINE_STRIP);
  124.     glVertex2f(-windW/2, 0);
  125.     glVertex2f(windW/2, 0);
  126.     glEnd();
  127.     glBegin(GL_LINE_STRIP);
  128.     glVertex2f(0, -windH/2);
  129.     glVertex2f(0, windH/2);
  130.     glEnd();
  131.  
  132.     if (mode) {
  133.     glEnable(GL_BLEND);
  134.     glEnable(GL_POINT_SMOOTH);
  135.     } else {
  136.     glDisable(GL_BLEND);
  137.     glDisable(GL_POINT_SMOOTH);
  138.     }
  139.  
  140.     glPointSize(size);
  141.     if (mode) {
  142.     (rgb) ? glColor3f(1.0, 0.0, 0.0) : glIndexf(CI_ANTI_ALIAS_RED);
  143.     } else {
  144.     (rgb) ? glColor3f(1.0, 0.0, 0.0) : glIndexf(CI_RED);
  145.     }
  146.     glBegin(GL_POINTS);
  147.     glVertex3fv(point);
  148.     glEnd();
  149.  
  150.     glDisable(GL_POINT_SMOOTH);
  151.     glDisable(GL_BLEND);
  152.  
  153.     glPointSize(1);
  154.     TK_SETCOLOR(windType, TK_GREEN);
  155.     glBegin(GL_POINTS);
  156.     glVertex3fv(point);
  157.     glEnd();
  158.  
  159.     glFlush();
  160.  
  161.     if (doubleBuffer) {
  162.     tkSwapBuffers();
  163.     }
  164. }
  165.  
  166. static GLenum Args(int argc, char **argv)
  167. {
  168.     GLint i;
  169.  
  170.     rgb = GL_TRUE;
  171.     doubleBuffer = GL_FALSE;
  172.     directRender = GL_TRUE;
  173.  
  174.     for (i = 1; i < argc; i++) {
  175.     if (strcmp(argv[i], "-ci") == 0) {
  176.         rgb = GL_FALSE;
  177.     } else if (strcmp(argv[i], "-rgb") == 0) {
  178.         rgb = GL_TRUE;
  179.     } else if (strcmp(argv[i], "-sb") == 0) {
  180.         doubleBuffer = GL_FALSE;
  181.     } else if (strcmp(argv[i], "-db") == 0) {
  182.         doubleBuffer = GL_TRUE;
  183.     } else if (strcmp(argv[i], "-dr") == 0) {
  184.         directRender = GL_TRUE;
  185.     } else if (strcmp(argv[i], "-ir") == 0) {
  186.         directRender = GL_FALSE;
  187.     } else {
  188.         printf("%s (Bad option).\n", argv[i]);
  189.         return GL_FALSE;
  190.     }
  191.     }
  192.     return GL_TRUE;
  193. }
  194.  
  195. void main(int argc, char **argv)
  196. {
  197.  
  198.     if (Args(argc, argv) == GL_FALSE) {
  199.     tkQuit();
  200.     }
  201.  
  202.     windW = 300;
  203.     windH = 300;
  204.     tkInitPosition(0, 0, windW, windH);
  205.  
  206.     windType = (rgb) ? TK_RGB : TK_INDEX;
  207.     windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  208.     windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  209.     tkInitDisplayMode(windType);
  210.  
  211.     if (tkInitWindow("Point Test") == GL_FALSE) {
  212.     tkQuit();
  213.     }
  214.  
  215.     Init();
  216.  
  217.     tkExposeFunc(Reshape);
  218.     tkReshapeFunc(Reshape);
  219.     tkKeyDownFunc(Key);
  220.     tkDisplayFunc(Draw);
  221.     tkExec();
  222. }
  223.